package com.systemframework.sys.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.systemframework.annotation.LicenseAnnotation;
import com.systemframework.annotation.LogAnnotation;
import com.systemframework.annotation.OperateAnnotation;
import com.systemframework.common.BaseCommonInterface;
import com.systemframework.enums.BusinessType;
import com.systemframework.sys.dto.request.EmailTemplateRequestDTO;
import com.systemframework.sys.dto.response.EmailTemplateResponseDTO;
import com.systemframework.sys.entity.EmailTemplate;
import com.systemframework.sys.service.EmailTemplateService;
import com.systemframework.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author summer
 * @date 2023-04-06 10:39
 */
@RestController
@RequestMapping(value = "/emailTemplate")
public class EmailTemplateController implements BaseCommonInterface<EmailTemplateRequestDTO, EmailTemplateResponseDTO> {

    private EmailTemplateService emailTemplateService;

    @Autowired
    public void setEmailTemplateService(EmailTemplateService emailTemplateService) {
        this.emailTemplateService = emailTemplateService;
    }

    @LicenseAnnotation
    @OperateAnnotation(businessType = BusinessType.INSERT,name = "邮箱模板")
    @Override
    public Result<String> save(@Valid EmailTemplateRequestDTO emailTemplateRequestDTO) {
        emailTemplateService.save(Convert.convert(EmailTemplate.class,emailTemplateRequestDTO));
        return Result.successResult();
    }

    @LicenseAnnotation
    @OperateAnnotation(businessType = BusinessType.UPDATE,name = "邮箱模板")
    @Override
    public Result<String> update(EmailTemplateRequestDTO emailTemplateRequestDTO) {
        emailTemplateService.updateById(Convert.convert(EmailTemplate.class,emailTemplateRequestDTO));
        return Result.successResult();
    }

    @LicenseAnnotation
    @OperateAnnotation(businessType = BusinessType.DELETE,name = "邮箱模板")
    @Override
    public Result<String> del(Integer id) {
        emailTemplateService.removeById(id);
        return Result.successResult();
    }

    @LicenseAnnotation
    @Override
    public Result<Page<EmailTemplateResponseDTO>> lists(EmailTemplateRequestDTO emailTemplateRequestDTO) {
        Page<EmailTemplateResponseDTO> searchResult=emailTemplateService.findLists(emailTemplateRequestDTO);
        return Result.successResult(searchResult);
    }



    @LicenseAnnotation
    @Override
    public Result<EmailTemplateResponseDTO> findById(Integer id) {
        return Result.successResult(Convert.convert(EmailTemplateResponseDTO.class,emailTemplateService.getById(id)));
    }

    @LicenseAnnotation
    @OperateAnnotation(businessType = BusinessType.BATCHDELETE,name = "邮箱模板")
    @Override
    public Result<String> batchDel(List<EmailTemplateResponseDTO> lists) {
        List<Integer> ids = lists.stream().map(EmailTemplateResponseDTO::getId).collect(Collectors.toList());
        if(ids.size()>0 && CollUtil.isNotEmpty(ids)){
            emailTemplateService.removeByIds(ids);
        }
        return Result.successResult();
    }




}
